<?php
class lib_sys_admin extends lib_base{
	public $table_name = "qc_sys_admin";
	public $pk = "id";

    public function admin_login($username,$password){
        $password = qcing_hash($password);
        $cond = "username='{$username}' and password='{$password}' and status=1";
        $admin = $this->find(array($cond));
        if($admin){
            // 生成token (session3rd)
            //通过认证后获取uid为304
            $uid = $admin['id'];
            $jwtAuth = new \lib\JwtAuth();
            // $jwtAuth -> getInstance();
            $token = $jwtAuth->setUid($uid)->encode()->getToken();

            $data['admin'] = $admin;
            $data['token'] = $token;
            return $data;
        }else{
            return false;
        }
    }


    public function pwd_user($data){
        $username = $data['username'];
        $cond = "username='{$username}' and status=1";
        $admin = $this->find(array($cond));
        if($admin){
            $arrBase = array(
                'password'=>qcing_hash($data['password']),
            );
            $this->edit($admin['id'], $arrBase);
            return true;
        }
        return false;
     }

    public function up_time($id){
       $arrBase = array(
            'up_time'=>date('Y-m-d H:i:s'),
            'last_ip'=>get_ip(),
        );
        return $this->edit($id,$arrBase);
    }

    public function check_user($username){
        $cond = "username='{$username}'";
        return $this->find(array($cond));
    }

    public function get_admin_token($token)
    {
        $open_id = pp_encrypt($token,'DECODE');
        $admin = $this->find(array("id = '{$open_id}'"));
        return $admin;
    }

}